import javax.swing.*;
import java.util.Arrays;
import java.awt.*;
import java.io.*; 
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;


public class Exemplo1 {
	
	public static JButton send;
	public static JButton pause;
	public static JButton add;
	public static JButton addPasta;
	public static JLabel label;
	public static String filme = new String();
	public static void main(String[] args) throws IOException{	
		final Server server = new Server();
		new Thread(server).start();
		JFrame frame = new JFrame("Pri");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setBounds(30, 30, 300, 300);
		Container content = frame.getContentPane(); // Get the content pane
		content.setLayout(new GridBagLayout());// Set the container layout mgr
	    GridBagConstraints c = new GridBagConstraints();    	
	    label = new JLabel("esperando");
		send = new JButton("send");					//cria os botoes
		pause = new JButton("pause");
		addPasta = new JButton("addPasta");
		add = new JButton("add");
		send.addActionListener(new MeuButao());
		pause.addActionListener(new play());
		add.addActionListener(new Add());
		addPasta.addActionListener(new AddPasta()); 
		c.gridx = 0;								//coloca os botoes na GridBagLayout
	    c.gridy = 0;
		content.add(pause,c);
		c.gridx = 2;
	    c.gridy = 0;
		content.add(send,c);
		c.gridx = 1;
	    c.gridy = 1;
		content.add(label,c);
		c.gridx = 0;
	    c.gridy = 2;
		content.add(add,c);
		c.gridx = 2;
	    c.gridy = 2;
		content.add(addPasta,c);
		frame.pack();
		frame.setVisible(true);
		
		EnviarLista enviar = new EnviarLista();
		//server.message="?filme:PUNCH_BLEACH_-_344_HD.mkv";
		while(true){
			if (server.message!=null && server.message.equals("?filmes")){  //Android esta pedindo pela lista de filmes
				enviar.Lista();
				server.message=null;
				try{
					   Thread.sleep(50);}
					catch (Exception e) {e.printStackTrace();}
					
			}
			if (server.message!=null && server.message.contains("?filme:")){
				filme = server.message.split(":")[1];
				AbrirFilme(filme);
				server.message=null;
				try{
					   Thread.sleep(50);}
					catch (Exception e) {e.printStackTrace();}
					
			}
			Exemplo1.label.setText(server.message);
			if (server.message!=null && server.message.equals("?play")){
				VLCControler.comando("pause");
				server.message=null;
				try{
					   Thread.sleep(50);}
					catch (Exception e) {e.printStackTrace();}
					
			}
			if (server.message!=null && server.message.equals("?stop")){
				VLCControler.comando("stop");
				server.message=null;
				try{
					   Thread.sleep(50);}
					catch (Exception e) {e.printStackTrace();}
					
			}
			try{
			   Thread.sleep(50);}
			catch (Exception e) {e.printStackTrace();}
			}
		
	}
	public static void AbrirFilme(String filme){
		Leitor leitor = new Leitor();
		String[] a= new String[50];
		String b= new String();
		int i,j;
		for(i=0;leitor.ler()[i]!=null;i++){ 
			a = leitor.ler()[i].split("\\\\");						
			b = a[0];
			for(j=1;!b.equals(leitor.ler()[i]);j++){				//separa somente o nome do arquivo
				b=(b + "\\" + a[j]);
			}
			if (a[j-1].equals(filme)){
				System.out.println(b);
				//
				
				try {
					Runtime rt = Runtime.getRuntime();
					//String[]callAndArgs= {"\"C:\\Program Files (x86)\\VideoLAN\\VLC\\vlc.exe\" -vvv -I rc "+"\""+ b +"\""+" --sout \"#transcode{vcodec=h264,vb=800,scale=1,acodec=mp4a,ab=128,channels=2,samplerate=44100}:std{access=http,mux=ts,dst=:8080/teste}\""};
					String callAndArgs= "\"C:\\Program Files\\VideoLAN\\VLC\\vlc\" -I rc ";
					//String[]callAndArgs= {"\"C:\\Program Files (x86)\\VideoLAN\\VLC\\vlc.exe\" -vvv -I rc \"C:\\video.avi\" --sout \"#transcode{vcodec=h264,vb=800,scale=1,acodec=mp4a,ab=128,channels=2,samplerate=44100}:std{access=http,mux=ts,dst=192.168.1.101:8080/teste}\""};
		            Process p=rt.exec(callAndArgs);
		            p.getInputStream().close(); p.getOutputStream().close(); p.getErrorStream().close();
		            //System.out.println(Arrays.toString(callAndArgs));
		    		VLCControler vlc = new VLCControler();
		            }
				
		            catch (IOException e) {
		                e.printStackTrace();
		            }				
				//
				//"C:\Program Files (x86)\VideoLAN\VLC\vlc.exe" -vvv -I rc "C:\video.avi" --sout "#transcode{vcodec=h264,vb=800,scale=1,acodec=mp4a,ab=128,channels=2,samplerate=44100}:std{access=http,mux=ts,dst=192.168.1.101:8080/teste}"
				//VLCControler.comando("add " + b);
				//VLCControler.comando("f");
				//System.out.println(b);
			}
		}
	}

}
